{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{if !empty($data)}修改{else}添加{/if}商品</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="form_basic.html#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" name="add_cate" id="add_goods" method="post" action="">
                
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品名称：</label>
                            <div class="input-group col-sm-4">
                                <input id="goods_name" type="text" class="form-control" name="goods_name" value="{$data.goods_name|default=''}">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品图片：</label>
                            <div class="controls uploadrow2" data-max="1" title="点击修改图片" rel="p_cover">
                                <input type="hidden" name="goods_thumb" id="cover_id_p_cover" value="{$data.goods_thumb|default=''}" data-callback="uploadImgCallback" value=""/>
                                <div class="upload-img-box" rel="img"  {if empty($data['goods_thumb'])} style="display:none" {/if} >
                                <div class="upload-pre-item2">
                                    <img width="100" height="100" src="{$data.goods_thumb|default=''}"/>
                                </div>
                                <em class="edit_img_icon">&nbsp;</em>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品类型：</label>
                            <div class="col-sm-4 m-l-n">
                                <select class="form-control" name="target_type" id="target_type">
                                    <option  value="">请选择</option>
                                    <option {if condition="@$data.target_type eq 1"}selected{/if} value="1">宝箱</option>
                                    <option {if condition="@$data.target_type eq 2"}selected{/if} value="2">道具</option>
                                </select>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">对象名称：</label>
                            <div class="col-sm-4 m-l-n">
                                <select class="form-control" name="target_id" id="target_id">
                                    {if condition="@$name"}
                                    {foreach $name as $val}
                                    {if condition="@$target_id"}
                                    <option {if condition="@$val['id'] eq $target_id['id']"}selected{/if} value="{$val['id']}">{$val['name']}</option>
                                    {/if}
                                    {/foreach}
                                    {/if}
                                </select>

                            </div>
                        </div>

                        <!--<div class="hr-line-dashed"></div>-->
                        <!--<div class="form-group">-->
                            <!--<label class="col-sm-3 control-label">是否推荐：</label>-->
                            <!--<div class="col-sm-4 m-l-n">-->
                                <!--<select class="form-control" name="is_best" id="is_best">-->
                                    <!--<option  value="">请选择</option>-->
                                    <!--<option {if condition="@$data.is_best eq 1"}selected{/if} value="1">是</option>-->
                                    <!--<option {if condition="@$data.is_best eq 0"}selected{/if} value="0" >否</option>-->
                                <!--</select>-->
                            <!--</div>-->
                        <!--</div>-->

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品价格：</label>
                            <div class="input-group col-sm-4">
                                <input id="goods_price" type="text" class="form-control" name="goods_price" value="{$data.goods_price|default=''}">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品数量：</label>
                            <div class="input-group col-sm-4">
                                <input id="goods_number" type="text" class="form-control" name="goods_number" value="{$data.goods_number|default=1}">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品排序：</label>
                            <div class="input-group col-sm-4">
                                <input id="sort" type="text" class="form-control" name="sort" value="{$data.sort|default=''}">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品描述：</label>
                            <div class="input-group col-sm-4">
                                <input id="goods_desc" type="text" class="form-control" name="goods_desc" value="{$data.goods_desc|default=''}">
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-3">
                                <button class="btn btn-primary" type="submit"><i class="fa fa-save"></i> 保存</button>&nbsp;&nbsp;&nbsp;
                                <a class="btn btn-danger" href="javascript:history.go(-1);"><i class="fa fa-close"></i> 返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>
{include file="public/footer" /}

<script type="text/javascript">

$(function(){
    
    //初始化上传图片插件
    initUploadImg();
});

function uploadImgCallback(name,id,src){

    $('#cover_id_p_cover').val(src);
}

//切换按钮
$('input[type="checkbox"][name=status]').change(function() {
    $('input[name=status]').val($(this).get(0).checked ? 1: 0);
});
</script>

<script type="text/javascript">

    $(function(){
        $('#add_goods').ajaxForm({
            beforeSubmit: checkForm,
            success: complete,
            dataType: 'json'
        });
        
        function checkForm(){
            // if( '' == $.trim($('#type').val())){
            //     layer.msg('请输入奖励类型',{icon:2,time:1500,shade: 0.1}, function(index){
            //     layer.close(index);
            //     });
            //     return false;
            // }cover_id_p_cover
            if( '' == $.trim($('#goods_name').val())){
                layer.msg('请输入商品名称',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#cover_id_p_cover').val())){
                layer.msg('请上传商品图片',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#target_type').val())){
                layer.msg('请选择商品类型',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#target_id').val())){
                layer.msg('请选择对象名称',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            // if( '' == $.trim($('#is_best').val())){
            //     layer.msg('请选择是否推荐',{icon:2,time:1500,shade: 0.1}, function(index){
            //         layer.close(index);
            //     });
            //     return false;
            // }
            if( '' == $.trim($('#goods_price').val())){
                layer.msg('请输入商品价格',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#goods_number').val())){
                layer.msg('请输入商品数量',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#sort').val())){
                layer.msg('请输入商品排序',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#goods_desc').val())){
                layer.msg('请输入商品描述',{icon:2,time:1500,shade: 0.1}, function(index){
                layer.close(index);
                });
                return false;
            }
        }

        function complete(data){
            if(data.code==1){
                layer.msg(data.msg, {icon: 6,time:1500,shade: 0.1}, function(index){
                    window.location.href="{:url('goods/index')}";
                });
            }else{
                layer.msg(data.msg, {icon: 5,time:1500,shade: 0.1});
                return false;   
            }
        }
     
    });

    //IOS开关样式配置
   var elem = document.querySelector('.js-switch');
        var switchery = new Switchery(elem, {
            color: '#1AB394'
        });
    var config = {
        '.chosen-select': {},                    
    }
    for (var selector in config) {
        $(selector).chosen(config[selector]);
    }

    //
    $("#target_type").on('change', function () {
        var rewardurl = "{:url('Goods/onload')}";
        var rids = $("#target_type").prop('value');
//        console.log(rids);
        $.ajax({
            type:'post',
            url:rewardurl,
            data:{rid:rids},
            dataType:'json',
            success:function (data) {
                console.log(data);
                var s = '';
                $.each(data, function(m,n){
                    s += '<option value="'+n.id+'">'+n.name+'</option>';
                });
                $("#target_id").children().remove();
                $("#target_id").append(s);
            }
        });
    });

</script>
</body>
</html>